/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aburakc.deliver;

import aburakc.MainMIDlet;
import aburakc.message.ConfirmMessage;
import aburakc.message.GeneralMessage;
import aburakc.message.SMSTextMessage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author burak.costu
 */
public class SMSDeliver implements Runnable {

    private Vector messageList = new Vector();
    private static Thread thread;
    private static SMSDeliver instance;
    private boolean destroy = false;
    private MainMIDlet mainMIDlet;

    /**
     * @return the mainMIDlet
     */
    public MainMIDlet getMainMIDlet() {
        return mainMIDlet;
    }

    /**
     * @param mainMIDlet the mainMIDlet to set
     */
    public void setMainMIDlet(MainMIDlet mainMIDlet) {
        this.mainMIDlet = mainMIDlet;
    }

    private SMSDeliver() {
    }

    public static SMSDeliver getInstance() {
        if (instance == null) {
            instance = new SMSDeliver();
            thread = new Thread(instance);
            thread.start();
        }

        return instance;
    }

    public void addMesssage(GeneralMessage message) {
        messageList.addElement(message);
    }

    private String sendNetworkMessage(String address, String message) {
        HttpConnection connection = null;
        String retVal = null;

        try {
            connection = (HttpConnection) Connector.open(address);
            //HTTP Request
            connection.setRequestProperty(
                    "User-Agent", "Profile/MIDP-1.0, Configuration/CLDC-1.0");

            connection.setRequestProperty("Content-Language", "en-US");
            connection.setRequestMethod(HttpConnection.POST);
            DataOutputStream os =
                    (DataOutputStream) connection.openDataOutputStream();

            os.writeUTF(message);
            os.flush();
            os.close();

            // Get the response from the servlet page.
            DataInputStream is = (DataInputStream) connection.openDataInputStream();
            //is = c.openInputStream();
            int ch;
            StringBuffer sb = new StringBuffer();
            while ((ch = is.read()) != -1) {
                sb.append((char) ch);
            }
            System.out.println(sb.toString());
            is.close();
            connection.close();

            retVal = sb.toString();

        } catch (IOException ex) {
            //ex.printStackTrace();
        }

        return retVal;

    }

    private void newSmsMessage(SMSTextMessage message) {
        sendNetworkMessage("http://localhost:8888/smsPost", message.toNetworkMessage());
    }

    private void deliveryMessage(ConfirmMessage message) {
        sendNetworkMessage("http://localhost:8888/deliverPost", message.toNetworkMessage());
    }

    public void run() {
        while (!isDestroy()) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            while (messageList.size() > 0) {
                try {
                    mainMIDlet.addScreenMessage(((GeneralMessage)messageList.firstElement()).getMessageId() + " servera  Gonderilecek");
                    if (messageList.firstElement() instanceof SMSTextMessage) {
                        newSmsMessage((SMSTextMessage) messageList.firstElement());
                    } else {
                        deliveryMessage((ConfirmMessage) messageList.firstElement());
                    }
                } finally {
                    messageList.removeElementAt(0);
                }
            }
        }
    }

    /**
     * @return the destroy
     */
    public boolean isDestroy() {
        return destroy;
    }

    /**
     * @param destroy the destroy to set
     */
    public void setDestroy(boolean destroy) {
        this.destroy = destroy;
    }

    public void destroy() {
        setDestroy(true);
        messageList.removeAllElements();
        messageList = null;
    }
}
